package com.example.smartcity_ikun3.ui.person

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.view.menu.ListMenuItemView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.base.BaseFragment
import com.example.smartcity_ikun3.logic.model.LoginAllModel
import com.example.smartcity_ikun3.logic.network.RetrofitService.Companion.create
import com.example.smartcity_ikun3.logic.network.SmartApi
import com.example.smartcity_ikun3.logic.util.Util.guide
import com.example.smartcity_ikun3.logic.util.Util.show
import com.example.smartcity_ikun3.ui.App
import com.example.smartcity_ikun3.ui.home.HomeFragment
import com.example.smartcity_ikun3.ui.person.activity.*
import kotlinx.android.synthetic.main.fragment_notifications.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class NotificationsFragment : BaseFragment() {

    private val viewModel by lazy {
        ViewModelProviders.of(this).get(NotificationsViewModel::class.java)
    }


    private lateinit var mPersonListView: ListView
    private lateinit var mLoginOk: Button
    private lateinit var mLoginName: TextView
    private lateinit var mUserImageView: ImageView



    private lateinit var root: View
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        root = inflater.inflate(R.layout.fragment_notifications, container, false)
        mPersonListView = root.findViewById(R.id.personListView)
        mLoginOk = root.findViewById(R.id.loginOk)
        mLoginName = root.findViewById(R.id.loginName)
        mUserImageView = root.findViewById(R.id.userImageView)



        // list
        setListView()

        // 登录
        setLoginOK()

        setBtnOnClick()

        return root
    }

    /**
     * 监听
     */
    private fun setBtnOnClick() {

        mLoginOk.setOnClickListener {
            if (mLoginOk.text == "登录"){
                startActivity(Intent(context, LoginAddActivity::class.java))
            }

            if (mLoginOk.text == "退出"){
                context?.getSharedPreferences("token", Context.MODE_PRIVATE)?.edit().apply {
                    this?.putBoolean("user", false)
                    this?.putString("token", "")
                    this?.apply()
                    onStart()
                }
            }
        }
    }

    /**
     * 登录
     */
    private fun setLoginOK() {
        // 获取状态

    }

    private fun setListView() {
        val array = listOf<String>(
            "个人信息",
            "修改密码",
            "交费记录",
            "意见反馈"
        )

        val arrayAdapter = ArrayAdapter(context!!, android.R.layout.simple_list_item_1, array)
        mPersonListView.adapter = arrayAdapter
        mPersonListView.setOnItemClickListener { adapterView, view, position, l ->
            when(position){
                0 -> startActivity(Intent(context, LoginAllActivity::class.java))
                1 -> startActivity(Intent(context, NewPasswordActivity::class.java))
                2 -> startActivity(Intent(context, UserListActivity::class.java))
                3 -> startActivity(Intent(context, ViewActivity::class.java))
            }
        }
    }

    override fun getLayout(): Int = R.layout.fragment_notifications

    override fun initData() {

    }

    override fun initView() {
   }

    override fun initEvent() {

    }


    /**
     * 判断是否已经登录
     */
    override fun onStart() {
        super.onStart()
        context?.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
            if (this?.getBoolean("user", false) == true){
                mLoginOk.text = "退出"
                context?.getSharedPreferences("token", Context.MODE_PRIVATE).apply {
                    val token = this?.getString("token", "").toString()
                    create<SmartApi>().getLoginAll(token).enqueue(object : Callback<LoginAllModel>{
                        override fun onFailure(p0: Call<LoginAllModel>, p1: Throwable) {
                            "获取用户信息失败！".show()
                            Log.d("NET", "onFailure: login --> ${p1.message}")
                        }

                        override fun onResponse(p0: Call<LoginAllModel>, p1: Response<LoginAllModel>) {
                            val body = p1.body()
                            if (body != null) {
                                if (body.code == 200){
                                    val img = "http://124.93.196.45:10002" + body.user?.avatar.toString()
                                    guide(img, mUserImageView)
                                    mLoginName.text = body.user?.nickName
                                }else "加载用户信息失败！".show()
                            }
                        }

                    })
                }

            }else{
                mLoginOk.text = "登录"
                mLoginName.text = "您还没有登录"
            }
        }
    }
}